package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.linq.cool.beauty.domain.BeautyBrandProductCategory;
import com.linq.cool.beauty.domain.BeautyProductCategory;
import com.linq.cool.beauty.dto.BeautyProductCategoryQueryDTO;
import com.linq.cool.beauty.service.BeautyBrandProductCategoryService;
import com.linq.cool.beauty.service.BeautyProductCategoryService;
import com.linq.cool.beauty.vo.BeautyProductCategoryTreeVO;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.BeanUtils;
import com.linq.cool.common.core.utils.TreeUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yqlin
 * @Date: 2021/2/5 13:51
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("productCategory")
public class BeautyProductCategoryController extends BaseController {
    @Autowired
    private BeautyProductCategoryService beautyProductCategoryService;
    @Autowired
    private BeautyBrandProductCategoryService beautyBrandProductCategoryService;

    @PreAuthorize(hasPerm = "beauty:productCategory:list")
    @GetMapping("list")
    public Result<?> list(BeautyProductCategoryQueryDTO dto) {
        List<BeautyProductCategory> beautyProductCategoryList = beautyProductCategoryService.listByParams(dto);
        return Result.ok(beautyProductCategoryList);
    }

    @GetMapping(value = "{productCategoryId}")
    public Result<?> getInfo(@PathVariable("productCategoryId") Long productCategoryId) {
        return Result.ok(beautyProductCategoryService.getById(productCategoryId));
    }

    @Log(moduleTitle = "商品分类", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "beauty:productCategory:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody BeautyProductCategory productCategory) {
        return this.toResult(beautyProductCategoryService.saveProductCategory(productCategory));
    }

    @Log(moduleTitle = "商品分类", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:productCategory:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody BeautyProductCategory productCategory) {
        return this.toResult(beautyProductCategoryService.updateProductCategory(productCategory));
    }

    @Log(moduleTitle = "商品分类", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "beauty:productCategory:remove")
    @DeleteMapping("{productCategoryIds}")
    public Result<?> remove(@PathVariable("productCategoryIds") List<Long> productCategoryIds) {
        return this.toResult(beautyProductCategoryService.removeByIds(productCategoryIds));
    }

    /**
     * 获取商品分类下拉列表
     *
     * @return
     */
    @GetMapping("getOptionSelect")
    public Result<?> getOptionSelect() {
        return Result.ok(beautyProductCategoryService.list(new LambdaQueryWrapper<BeautyProductCategory>()
                                                                   .select(BeautyProductCategory::getId, BeautyProductCategory::getParentId, BeautyProductCategory::getName, BeautyProductCategory::getLevel, BeautyProductCategory::getIcon)
        ));
    }

    /**
     * 根据品牌id获取商品分类id集合
     */
    @GetMapping("getProductCategories/{brandId}")
    public Result<?> getProductCategoryByBrandId(@PathVariable("brandId") Long brandId) {
        List<BeautyBrandProductCategory> brandProductCategories = beautyBrandProductCategoryService.list(new LambdaQueryWrapper<BeautyBrandProductCategory>()
                                                                                                                 .eq(BeautyBrandProductCategory::getBrandId, brandId)
        );
        List<Long> productCategories = new ArrayList<>();
        for (BeautyBrandProductCategory brandProductCategory : brandProductCategories) {
            productCategories.add(brandProductCategory.getProductCategoryId());
        }
        return Result.ok(productCategories);
    }

    /**
     * 查询所有二级商品分类
     *
     * @return
     */
    @GetMapping("getSecondLevelProductCategories")
    public Result<?> getSecondLevelProductCategories() {
        return Result.ok(beautyProductCategoryService.getSecondLevelProductCategories());
    }

    /**
     * 查询首页导航宫格
     *
     * @return
     */
    @GetMapping("getNavList")
    public Result<?> getNavList() {
        return Result.ok(beautyProductCategoryService.listByNavStatus());
    }

    /**
     * 获取树状列表
     *
     * @return
     */
    @GetMapping("getTreeList")
    public Result<?> getTreeList() {
        List<BeautyProductCategory> list = beautyProductCategoryService.list(new LambdaQueryWrapper<BeautyProductCategory>()
                                                                                     .select(BeautyProductCategory::getId, BeautyProductCategory::getParentId, BeautyProductCategory::getName, BeautyProductCategory::getLevel, BeautyProductCategory::getIcon));
        if (CollectionUtils.isNotEmpty(list)) {
            List<BeautyProductCategoryTreeVO> voList = list.stream().map(e -> {
                BeautyProductCategoryTreeVO vo = new BeautyProductCategoryTreeVO();
                BeanUtils.copyProperties(e, vo);
                return vo;
            }).collect(Collectors.toList());
            return Result.ok(TreeUtils.toTreeList(voList, 0L, Long.class, BeautyProductCategoryTreeVO.class));
        }
        return Result.ok();

    }

    /**
     * 根据分类id 查询同级的分类列表 和 品牌列表
     *
     * @return
     */
    @GetMapping("getProductCategoriesAndBrands/{productCategoryId}")
    public Result<?> getProductCategoriesAndBrands(@PathVariable("productCategoryId") Long productCategoryId) {
        return Result.ok(beautyProductCategoryService.getProductCategoriesAndBrands(productCategoryId));
    }
}
